
#ifndef MATH_H_
#define MATH_H_
#include <math.h>
#endif


#include "TwRandomNumbers.h "


int g_random_seed = 22471;


void GaussRandomPair( 
	float& result_a, float& result_b,
	float dMean, float dStdDeviation)
{
	float x1, x2, w, y1, y2;

	do {
		 x1 = 2.0f * random_unit_float() - 1.0f;
		 x2 = 2.0f * random_unit_float() - 1.0f;
		 w = x1 * x1 + x2 * x2;
	} while ( w >= 1.0f );

	w = sqrtf( (-2.0f * logf( w ) ) / w );
	y1 = x1 * w;
	y2 = x2 * w;

    result_a = ( dMean + y1 * dStdDeviation );
    result_b = ( dMean + y2 * dStdDeviation );
}

float GaussRandom( float dMean, float dStdDeviation )
{
	static bool use_precalced_result = false;
	static float y2=0.0f;
	float x1, x2, w, y1;
	
	if (use_precalced_result)
	{
		use_precalced_result = false;
		return( dMean + y2 * dStdDeviation );
	}

	do {
		 x1 = 2.0f * random_unit_float() - 1.0f;
		 x2 = 2.0f * random_unit_float() - 1.0f;
		 w = x1 * x1 + x2 * x2;
	} while ( w >= 1.0f );

	w = sqrtf( (-2.0f * logf( w ) ) / w );
	y1 = x1 * w;
	y2 = x2 * w;

	use_precalced_result = true;

    return( dMean + y1 * dStdDeviation );
}

